#include "playscene.h"
#include "mypushbutton.h"

#include "dataconfig.h"

//PlayScene::PlayScene(QWidget *parent) : QMainWindow(parent)
//{

//}

PlayScene::PlayScene(int levelNum)
{
    this->levelIdx = levelNum;

    // 初始化游戏场景
    this->setFixedSize(350, 588);
    this->setWindowIcon(QIcon(":/res/Coin0001.png"));
    this->setWindowTitle("Flipping");

    // 菜单栏设置
    QMenuBar *mBar = menuBar();
    setMenuBar(mBar);
    QMenu *pStart = mBar->addMenu("Start");
    QAction *pQuit = pStart->addAction("Quit");

    connect(pQuit, &QAction::triggered, [=]() {
        this->close();
    });

    // 添加音效资源
    QSound *backSound = new QSound(":/res/BackButtonSound.wav", this);
    QSound *coinFlipSound = new QSound(":/res/ConFlipSound.wav", this);
    QSound *winSound = new QSound(":/res/LevelWinSound.wav", this);

    // 返回按钮
    MyPushButton *backBtn = new MyPushButton(":/res/BackButton.png", ":/res/BackButtonSelected.png");
    backBtn->setParent(this);
    backBtn->move(this->width() - backBtn->width(), this->height() - backBtn->height());
    connect(backBtn, &MyPushButton::clicked, [=]() {
        backSound->play();

        QTimer::singleShot(200, this, [=]() {
            emit this->backToLevelSceneSignal();
        });
    });

    // 显示关卡
    QLabel *label = new QLabel(this);
    QFont font;
    font.setFamily("华文新魏");
    font.setPointSize(20);
    QString fontStr = QString("Level: %1").arg(this->levelIdx);
    //将字体设置到标签
    label->setFont(font);
    label->setText(fontStr);
    label->setGeometry(30, this->height()-50, 150, 50);

    // 初始化每个关卡的二维数组
    dataConfig data;
    for(int i = 0; i < 4; ++i) {
        for(int j = 0; j < 4; ++j) {
            this->gameArray[i][j] = data.mData[this->levelIdx][i][j];
        }
    }

    // 胜利的图片
    QLabel *winLabel = new QLabel(this);
    QPixmap tmpPix;
    tmpPix.load(":/res/LevelCompletedDialogBg.png");
    winLabel->setGeometry(0, 0, tmpPix.width(), tmpPix.height());
    winLabel->setPixmap(tmpPix);
    winLabel->move((this->width()-tmpPix.width())*0.5, -tmpPix.height());

    // 显示金币背景图片
    for(int i = 0; i < 4; ++i) {
        for(int j = 0; j < 4; ++j) {
            QPixmap pix;
            pix.load(":/res/BoardNode(1).png");
            QLabel *label = new QLabel(this);
            label->setGeometry(0, 0, pix.width(), pix.height());
            label->setPixmap(pix);
            label->move(75 + i*50, 200 + j*50);

            // 创建金币
            QString str;
            if(this->gameArray[i][j] == 1) {
                str = ":/res/Coin0001.png";
            }else {
                str = ":/res/Coin0008.png";
            }

            MyCoin *coin = new MyCoin(str);
            // coin->setParent(label); // 可以选择父对象是label但是显示的位置不是居中的
            coin->setParent(this);
            coin->move(77 + i*50, 203 + j*50);

            coin->posX = i;
            coin->posY = j;
            coin->flag = gameArray[i][j];

            // 将金币放入 金币二维数组中以便于后期维护
            coinBtn[i][j] = coin;

            // 点击金币 进行翻转
            connect(coin, &MyCoin::clicked, [=]() {

                coinFlipSound->play();

                // 点击按钮 将其他按钮先禁用
                for(int i = 0; i < 4; ++i) {
                    for(int j = 0; j < 4; ++j) {
                        this->coinBtn[i][j]->isWin = true;
                    }
                }


                coin->changeFlag();

                // 延时翻转金币周围
                QTimer::singleShot(300, this, [=](){

                    if(coin->posX + 1 < 4) {    // 翻转鼠标点击的右侧金币
                        coinBtn[coin->posX + 1][coin->posY]->changeFlag();
                        this->gameArray[coin->posX + 1][coin->posY] = 1 - this->gameArray[coin->posX + 1][coin->posY];
                    }
                    if(coin->posX - 1 >= 0) {    // 翻转鼠标点击的左侧金币
                        coinBtn[coin->posX - 1][coin->posY]->changeFlag();
                        this->gameArray[coin->posX - 1][coin->posY] = 1 - this->gameArray[coin->posX - 1][coin->posY];
                    }
                    if(coin->posY + 1 < 4) {    // 翻转鼠标点击的上侧金币
                        coinBtn[coin->posX][coin->posY + 1]->changeFlag();
                        this->gameArray[coin->posX][coin->posY + 1] = 1 - this->gameArray[coin->posX][coin->posY + 1];
                    }
                    if(coin->posY - 1 >= 0) {    // 翻转鼠标点击的下侧金币
                        coinBtn[coin->posX][coin->posY - 1]->changeFlag();
                        this->gameArray[coin->posX][coin->posY - 1] = 1 - this->gameArray[coin->posX][coin->posY - 1];
                    }

                    // 点击按钮后，周围的金币都翻转之后 将其他按钮接触禁用
                    for(int i = 0; i < 4; ++i) {
                        for(int j = 0; j < 4; ++j) {
                            this->coinBtn[i][j]->isWin = false;
                        }
                    }

                    // 判断是否胜利
                    isWin = true;
                    for(int i = 0; i < 4; ++i) {
                        for(int j = 0; j < 4; ++j) {
                            if(coinBtn[i][j]->flag == false) {
                                this->isWin = false;
                                break;
                            }
                        }
                    }
                    if(this->isWin) {
                        winSound->play();
                        // 胜利之后，所有按钮都无法点击即再次点击按钮直接return不做响应
                        for(int i = 0; i < 4; ++i) {
                            for(int j = 0; j < 4; ++j) {
                                coinBtn[i][j]->isWin = true;
                            }
                        }

                        // 胜利的图片砸下来 动画设置
                        QPropertyAnimation *animation = new QPropertyAnimation(winLabel, "geometry");
                        animation->setDuration(1000);
                        animation->setStartValue(QRect(winLabel->x(), winLabel->y(), winLabel->width(), winLabel->height()));
                        animation->setEndValue(QRect(winLabel->x(), winLabel->y()+114, winLabel->width(), winLabel->height()));
                        animation->setEasingCurve(QEasingCurve::OutBounce);
                        animation->start();
                    }
                });

            });

        }
    }
}

void PlayScene::paintEvent(QPaintEvent *event)
{
    QPainter paint(this);

    QPixmap pix;
    pix.load(":/res/PlayLevelSceneBg.png");
    paint.drawPixmap(0, 0, this->width(), this->height(), pix);

    // 上方的背景
    pix.load(":/res/Title.png");
    pix.scaled(pix.width() * 0.5, pix.height() * 0.5);
    paint.drawPixmap(10, 30, pix);

}
